package com.gzdh.device.shop.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzdh.device.comm.util.HttpClientUtils;
import com.gzdh.device.comm.vo.Result;
import com.gzdh.device.shop.entity.ShopInfo;
import com.gzdh.device.shop.entity.ShopMachine;
import com.gzdh.device.shop.entity.TradeNo;
import com.gzdh.device.shop.service.ShopInfoService;
import com.gzdh.device.shop.service.ShopMachineService;
import com.gzdh.device.shop.service.TradeNoService;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/shop/v1")
public class ShopInfoController {


    @Autowired
    ShopInfoService shopInfoService;


    @Autowired
    ShopMachineService shopMachineService;

    @Autowired
    TradeNoService tradeNoService;


    @GetMapping("getShopInfo")
    public Result<Object> getShopInfo(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(name = "name", required = false) String name) {
        Result<Object> result = new Result<>();
        System.out.println("接收数值：" + pageNo);
        IPage<ShopInfo> shopInfoIPage = new Page<>(pageNo, pageSize);
        shopInfoIPage = shopInfoService.page(shopInfoIPage, new QueryWrapper<ShopInfo>().lambda().like(StringUtils.isNotEmpty(name),ShopInfo::getName, name));
        result.setResult(shopInfoIPage);
        return result;
    }

    /**
     * 获取全部商户列表
     *
     * @return
     */
    @GetMapping("getShopInfoList")
    public Result<Object> getShopInfoList() {
        Result<Object> result = new Result<>();
        List<ShopInfo> shopInfoList = shopInfoService.list();
        result.setResult(shopInfoList);
        return result;
    }

    @GetMapping("findShopInfoById")
    public Result<Object> findShopInfoById(@RequestParam("id") String id) {
        Result<Object> result = new Result<>();
        ShopInfo shopInfo = shopInfoService.getById(id);
        result.setResult(shopInfo);
        return result;
    }

    /**
     * 添加商户信息
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("addShop")
    public Result<Object> addShop(@RequestBody JSONObject jsonObject) {
        Result<Object> result = new Result<>();
        String name = jsonObject.getString("name");
        String businessUrl = jsonObject.getString("businessUrl");
        int counts = shopInfoService.count(new QueryWrapper<ShopInfo>().lambda().eq(ShopInfo::getName, name));
        if (counts > 0) {
            //存在相同商户名称
            return Result.error("该商户名称已存在，请核对后重新提交");
        }
        ShopInfo shopInfo = new ShopInfo();
        shopInfo.setName(name);
        shopInfo.setBusinessUrl(businessUrl);
        shopInfoService.save(shopInfo);
        ShopMachine shopMachine = getShopMachine(shopInfo);
        shopMachineService.save(shopMachine);
        return result;
    }

    @NotNull
    private static ShopMachine getShopMachine(ShopInfo shopInfo) {
        ShopMachine shopMachine = new ShopMachine();
        shopMachine.setShopId(shopInfo.getId());
        shopMachine.setBusinessUrl("http://");
        shopMachine.setIsvName("广州市鼎鸿电子科技有限公司");
        shopMachine.setPartnerId("2088121760636121");
        shopMachine.setMerchantId("2088441828867338");
        shopMachine.setMerchantName("广州市鼎鸿电子科技有限公司");
        shopMachine.setLogicGroupId("BUSINESS_2110410498703360");
        shopMachine.setAbcpAppid("2021003143621011");
        shopMachine.setAbcpAppVersion("1.0.0.0");
        return shopMachine;
    }

    /**
     * 更新商户信息
     *
     * @return
     */
    @PostMapping("updateShop")
    public Result<Object> updateShop(@RequestBody JSONObject jsonObject) {
        Result<Object> result = new Result<>();
        String id = jsonObject.getString("id");
        String name = jsonObject.getString("name");
        String businessUrl = jsonObject.getString("businessUrl");
        ShopInfo shopInfo = shopInfoService.getById(id);
        if (shopInfo == null) {
            return Result.error("商户信息不存在，请核对后添加");
        }
        shopInfo.setBusinessUrl(businessUrl);
        shopInfo.setName(name);
        shopInfoService.updateById(shopInfo);
        return result;
    }

    /**
     * 获取远程租户ID，请求远程接口进行返回前端操作
     *
     * @param id
     * @return
     */
    @GetMapping("getShopTenant")
    public Result<Object> getShopTenant(@RequestParam("shopId") String shopId) {
        Result<Object> result = new Result<>();
        ShopInfo shopInfo = shopInfoService.getById(shopId);
//        String str = "";
        String str = HttpClientUtils.doPost(shopInfo.getBusinessUrl() + "/api/system/v3/tenant/getTenant", new HashMap<>(), "123456789987654321");
        result.setResult(JSONObject.parseObject(str).getJSONArray("result"));

//        Map<String, String> map = new HashMap<>();
//        map.put("channel", "0758514075");
//        map.put("date", "2023-12-22");
//        str = HttpClientUtils.doPost("http://127.0.0.1:9003/api/system/v3/leshua/getOrdersBill", map, "123456789987654321");
//        JSONArray jsonArray = JSONObject.parseObject(str).getJSONArray("result");
//        List<TradeNo> tradeNoList = jsonArray.toJavaList(TradeNo.class);
//        tradeNoService.saveOrUpdateBatch(tradeNoList);
//        System.out.println(str);
        return result;
    }
}
